/*
 * Created on 2007-3-7
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ims.iaa;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * @author changsheng
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class ManualPoolingDataSource {
    private static ManualPoolingDataSource s_instance = null;
    private static BasicDataSource dataSource = null;
    
    public static ManualPoolingDataSource getInstance()
    { 
		if ( s_instance == null )
		{
			s_instance = new ManualPoolingDataSource();
		}
		return s_instance;
   }
    
   private ManualPoolingDataSource() {
	    Properties dbProps = new Properties();
	
		try{
			dbProps.load(new FileInputStream("security\\db.properties"));
		} catch (IOException e)  {
			IaaUtil.log("Can not open the database properties file,IOException:" + e);
		}
	    try {
		    String driveClassName = dbProps.getProperty("db.driverName");
		    String url = dbProps.getProperty("db.url");
		    String username = dbProps.getProperty("db.userName");
		    String password = dbProps.getProperty("db.password");
		     
		    String initialSize = dbProps.getProperty("dataSource.initialSize");
		    String minIdle = dbProps.getProperty("dataSource.minIdle");
		    String maxIdle = dbProps.getProperty("dataSource.maxIdle");
		    String maxWait = dbProps.getProperty("dataSource.maxWait");
		    String maxActive = dbProps.getProperty("dataSource.maxActive");
//++changsheng 2007-3-8
		    String validationQuery=dbProps.getProperty("dataSource.validationQuery","SELECT 1");
		    boolean testOnBorrow=Boolean.valueOf(dbProps.getProperty("dataSource.testOnBorrow","true")).booleanValue();
		    boolean testOnReturn=Boolean.valueOf(dbProps.getProperty("dataSource.testOnReturn","false")).booleanValue();
		    boolean poolPreparedStatements=Boolean.valueOf(dbProps.getProperty("dataSource.poolPreparedStatements","false")).booleanValue(); ;
		    
//--
		    boolean logAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.logAbandoned","false"))).booleanValue();
			boolean removeAbandoned = (Boolean.valueOf(dbProps.getProperty("dataSource.removeAbandoned","false"))).booleanValue();
			int removeAbandonedTimeout = Integer.parseInt(dbProps.getProperty("dataSource.removeAbandonedTimeout","300"));
		    
			dataSource = new BasicDataSource();
		   	dataSource.setDriverClassName(driveClassName);
		   	dataSource.setUrl(url);
		   	dataSource.setUsername(username);
		   	dataSource.setPassword(password);
		   	if(initialSize!=null)
		   	    dataSource.setInitialSize(Integer.parseInt(initialSize));
		   	if(minIdle!=null)
		   	    dataSource.setMinIdle(Integer.parseInt(minIdle));
		   	if(maxIdle!=null)
		   	    dataSource.setMaxIdle(Integer.parseInt(maxIdle));
		   	if(maxWait!=null)
		   	    dataSource.setMaxWait(Long.parseLong(maxWait));
		   	if(maxActive!=null){
		   	    if(!maxActive.trim().equals("0"))
		   	        dataSource.setMaxActive(Integer.parseInt(maxActive));
		   	}
		   	
		    dataSource.setLogAbandoned(logAbandoned);
		    dataSource.setRemoveAbandoned(removeAbandoned);
		    dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
//++changsheng 2007-3-8
		    dataSource.setValidationQuery(validationQuery);
		    dataSource.setTestOnBorrow(testOnBorrow);
		    dataSource.setTestOnReturn(testOnReturn);
		    dataSource.setPoolPreparedStatements(poolPreparedStatements);
//--		    
		}
		catch (Exception e) {
		    IaaUtil.log("Create pool error:" + e);
		}
	}
   
   public Connection getConnection() throws SQLException{
       if(dataSource == null )
           return null ;
       return dataSource.getConnection() ;
   }
   
   public static void main(String args[]) throws SQLException{
       ManualPoolingDataSource instance =ManualPoolingDataSource.getInstance();
       Connection conn = instance.getConnection() ;
       Statement stmt = conn.createStatement() ;
       ResultSet rs = stmt.executeQuery("select * from tblCompany") ;
       while(rs.next()){
           System.out.println(rs.getString(1));
       }
       
   }
}